package com.zwl.springcloud.service.impl;



import com.zwl.springcloud.mobileApppayment.GoodsTradeManager;
import com.zwl.springcloud.service.MobileApppaymentService;
import org.springframework.stereotype.Service;
import org.yangyuan.pay.bean.AliTrade;
import org.yangyuan.pay.bean.common.Trade;
import org.yangyuan.pay.bean.common.TradeToken;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝app支付相关api(服务类)
 */
@Service
public class MobileApppaymentServiceImpl implements MobileApppaymentService {

    private GoodsTradeManager goodsTradeManager;
    /**
     * 发起支付
     * @param params 入参
     * @Value title(String)：商品标题，
     * describe（String）:商品描述，
     * uiqueStatus（String）:用户唯一标识
     * price(float):价格
     * @return
     */
    @Override
    public Map<String, Object> getUrl(Map<String, Object> params) {
        //商品标题
        String title = params.get("title") != null? params.get("title").toString():"";
        //商品描述
        String describe = params.get("describe") != null? params.get("describe").toString():"";
        //用户唯一标识
        String uiqueStatus = params.get("uiqueStatus") != null? params.get("uiqueStatus").toString():"";
        //price
        String price = params.get("price") != null? params.get("price").toString():"0.0";

        Map<String,Object> map = new HashMap<>();
        Trade trade = AliTrade
        .qrcodePay()
        .subject(title)
        .body(describe)
        .outTradeNo(goodsTradeManager.newTradeNo(uiqueStatus)).totalAmount(price)
        .build();
    TradeToken<String> token = goodsTradeManager.qrcodePay(trade);
    String url = token.value();
    map.put("url", url);
    return map;
    }
}
